/*
 * Copyright (C) 2015-2016  Nicola Spanti (RyDroid) <dev@nicola-spanti.info>
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 * 
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
 */


#include "PlanetWars2dRT-core/specific/controller/ship.h"
#include "PlanetWars2dRT-core/specific/model/ship_position.h"
#include "PlanetWars2dRT-core/specific/model/ship_move.h"


EXTERN_C_BEGIN


bool
ship_move_once_no_notify(ship_t* a_ship)
{
  const float distance_x =
    ((float) ship_get_top_left_position_to_go_x(a_ship))
    - a_ship->position_current.x;
  const float distance_y =
    ((float) ship_get_top_left_position_to_go_y(a_ship))
    - a_ship->position_current.y;
  if(distance_x == 0 && distance_y == 0)
    {
      return false;
    }
  
  const float distance = sqrt(distance_x * distance_x + distance_y * distance_y);
  const float factor = ((float) ship_get_moving_vector(a_ship)) / distance;
  
  if(factor < 1)
    {
      a_ship->position_current.x += factor * distance_x;
      a_ship->position_current.y += factor * distance_y;
    }
  else
    {
      a_ship->position_current.x = ship_get_top_left_position_to_go_x(a_ship);
      a_ship->position_current.y = ship_get_top_left_position_to_go_y(a_ship);
    }
  return true;
}

bool
ship_move_once(ship_t* a_ship)
{
  if(ship_move_once_no_notify(a_ship))
    {
      ship_notify_moved(a_ship);
      return true;
    }
  return false;
}

unsigned short
ship_move_no_notify(ship_t* a_ship, unsigned short nb_times)
{
  for(unsigned short i=0; i < nb_times; ++i)
    {
      if(!ship_move_once_no_notify(a_ship))
	{
	  return i;
	}
    }
  return nb_times;
}

unsigned short
ship_move(ship_t* a_ship, unsigned short nb_times)
{
  for(unsigned short i=0; i < nb_times; ++i)
    {
      if(!ship_move_once(a_ship))
	{
	  return i;
	}
    }
  return nb_times;
}


void
ship_move_once_no_notify_returns_void(ship_t* a_ship)
{
  ship_move_once_no_notify(a_ship);
}

void
ship_move_once_returns_void(ship_t* a_ship)
{
  ship_move_once(a_ship);
}

void
ship_move_no_notify_returns_void(ship_t* a_ship, unsigned short nb_times)
{
  ship_move_no_notify(a_ship, nb_times);
}

void
ship_move_returns_void(ship_t* a_ship, unsigned short nb_times)
{
  ship_move(a_ship, nb_times);
}


void
ship_update_once(ship_t* a_ship)
{
  ship_move_once(a_ship);
}

void
ship_update(ship_t* a_ship, unsigned short nb_times)
{
  for(unsigned short i=0; i < nb_times; ++i)
    {
      ship_update_once(a_ship);
    }
}


EXTERN_C_END
